﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MathService
{
    public class ArgumentsString
    {
        /* ------------------------------------------------------------------------------------------ */

        public static event EventHandler<ClassInitializeEventArgs> ClassInitialize;

        public class ClassInitializeEventArgs : EventArgs
        {
            private char f_TermsSeparator = ' ';
            private char f_ArgumentsSeparator = ':';
            private readonly List<char> f_KeyCharsList = new List<char> { '/', '-' };

            public char TermsSeparator { get { return f_TermsSeparator; } set { f_TermsSeparator = value; } }
            public char ArgumentsSeparator { get { return f_ArgumentsSeparator; } set { f_ArgumentsSeparator = value; } }
            public List<char> KeyCharsList { get { return f_KeyCharsList; } }
        }

        /* ------------------------------------------------------------------------------------------ */

        private static char sf_TermsSeparator;
        private static char sf_ArgumentsSeparator;
        private static readonly List<char> sf_KeyCharsList;

        /* ------------------------------------------------------------------------------------------ */

        private readonly string[] f_Arguments;

        /* ------------------------------------------------------------------------------------------ */

        public int Count { get { return f_Arguments == null ? 0 : f_Arguments.Length; } }

        public string this[int index] { get { return f_Arguments[index]; } }

        /* ------------------------------------------------------------------------------------------ */

        static ArgumentsString()
        {
            var event_args = new ClassInitializeEventArgs();
            ClassInitialize.FastStart(typeof(ArgumentsString), event_args);

            sf_TermsSeparator = event_args.TermsSeparator;
            sf_ArgumentsSeparator = event_args.ArgumentsSeparator;
            sf_KeyCharsList = event_args.KeyCharsList;
        }

        public ArgumentsString(string[] Arguments)
        {
            f_Arguments = Arguments;
        }

        /* ------------------------------------------------------------------------------------------ */

        public override string ToString()
        {
            if(f_Arguments == null) return "";
            var last_index = f_Arguments.Length - 1;
            return f_Arguments.Aggregate(new StringBuilder(), (S, s, i) => S.AppendFormat(i != last_index ? "{0} " : "{0}", s))
                .ToString();
        }

        /* ------------------------------------------------------------------------------------------ */

        public static implicit operator ArgumentsString(string[] Arguments) { return new ArgumentsString(Arguments); }

        public static implicit operator string[](ArgumentsString Arguments) { return Arguments.f_Arguments; }

        public static explicit operator string(ArgumentsString Argument) { return Argument.ToString(); }

        /* ------------------------------------------------------------------------------------------ */
    }
}
